//
//  ExpressReceiveViewController.m
//  TengyunStation
//
//  Created by Johnny on 15/8/24.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "ExpressReceiveViewController.h"
#import "ExpressDetailViewController.h"
#import "RecieveAlertView.h"
#import "NSString+Category.h"
#import "Singleton.h"
#import "UploadExpress.h"
#import "ReceivedCell.h"

@interface ExpressReceiveViewController ()<RecieveAlertViewDelegate,ExpressDetailViewControllerDelegate> {
    NSMutableArray *_expressList;
    BOOL _isUploaded;
    int _inwayExpressCount;
}

@end

@implementation ExpressReceiveViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"快递接收";
        _expressList = [NSMutableArray arrayWithCapacity:1];
        _isUploaded = NO;
        _inwayExpressCount = 0;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self rightBarButtonItemTitle:@"手动输入" Method:@selector(manualInput)];
    [self reloadNumberLabelWithNumber:0];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [_scanView startScanning];
    [self.tableView reloadData];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [_scanView stopScanning];
}

//手动输入
- (void)manualInput {
    [self showReceiverAlertViewWithExpressId:nil];
}

- (void)showReceiverAlertViewWithExpressId:(NSString *)expressId {
    [_scanView stopScanning];
    UploadExpress *uploadExpress = [[UploadExpress alloc] init];
    uploadExpress.batchNo = _batchNo;
    uploadExpress.inwayExpressId = [self.batchNo appendNumber:_inwayExpressCount ++];
    uploadExpress.expressId = expressId;
    uploadExpress.expressSendType = self.expressSendType;
    uploadExpress.expresser = self.expresser;
    RecieveAlertView *alert = [[RecieveAlertView alloc] initWithUploadExpress:uploadExpress];
    alert.codeList = _scanView.codeList;
    alert.delegate = self;
    [alert show];
}

//刷新已接收多少件
- (void)reloadNumberLabelWithNumber:(NSInteger)number {
    NSString *numberString = [NSString stringWithFormat:@"%d",(int)number];
    NSString *string = [NSString stringWithFormat:@"已接收 %@ 件",numberString];
    NSRange range = [string rangeOfString:numberString];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
    [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:range];
    _numberLabel.attributedText = attrString;
}

#pragma mark ScanViewDelegate
- (void)didFinishScanCode:(NSString *)code {
    [self showReceiverAlertViewWithExpressId:code];
}

#pragma mark RecieveAlertViewDelegate
- (void)cancelUploadExpress {
    [_scanView startScanning];
}

- (void)uploadExpressSuccess:(UploadExpress *)express {
    [_scanView.codeList addObject:express.expressId];
    if (!_isUploaded) {
        _isUploaded = YES;
        NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
        [mutableArray removeObjectAtIndex:1];
        self.navigationController.viewControllers = mutableArray;
    }
    [_expressList insertObject:express atIndex:0];
    [self.tableView reloadData];
    [self reloadNumberLabelWithNumber:_expressList.count];
    [_scanView startScanning];
}

#pragma mark UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _expressList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIndentifier = @"ReceivedCell";
    ReceivedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ReceivedCell" owner:self options:nil] lastObject];
    }
    UploadExpress *uploadExpress = [_expressList objectAtIndex:indexPath.row];
    cell.expressIdLabel.text = [NSString stringWithFormat:@"运单号:%@",uploadExpress.expressId];
    cell.phoneLabel.text = uploadExpress.receiver.telephone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 38.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    footerView.backgroundColor = [UIColor clearColor];
    return footerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UploadExpress *uploadExpress = [_expressList objectAtIndex:indexPath.row];
    ExpressDetailViewController *expressDVC = [[ExpressDetailViewController alloc] initWithNibName:@"ExpressDetailViewController" bundle:nil];
    expressDVC.inwayExpressId = uploadExpress.inwayExpressId;
    expressDVC.delegate = self;
    expressDVC.indexPath = indexPath;
    [self.navigationController pushViewController:expressDVC animated:YES];
}

#pragma mark ExpressDetailViewControllerDelegate
- (void)smsSendSuccess:(NSIndexPath *)indexPath {
    
}

- (void)takeExpressSuccess:(NSIndexPath *)indexPath {
    
}

- (void)updateExpress:(NSIndexPath *)indexPath Telephone:(NSString *)telephone Address:(NSString *)address {
    UploadExpress *uploadExpress = [_expressList objectAtIndex:indexPath.row];
    uploadExpress.receiver.telephone = telephone;
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)dealloc {
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
    [dic setObject:self.batchNo forKey:@"batchNo"];
    [dic setObject:[NSNumber numberWithInt:self.expressSendType] forKey:@"expressType"];
    [dic setObject:self.expresser.expresserId forKey:@"expresserId"];
    if (_expressList.count > 0) {
        [dic setObject:[NSNumber numberWithBool:YES] forKey:@"isSms"];//是否发送短信
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"positionfreenotification" object:nil userInfo:dic];
    _expressList = nil;
    self.expresser = nil;
}

@end
